package com.mjt.admin.controller;


import com.mjt.admin.model.RespBean;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.Role;
import com.mjt.admin.query.RoleQuery;
import com.mjt.admin.service.IRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 莫某人
 * @since 2021-10-27
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;


    /**
     * @description 跳转到角色管理主页
     * @author 莫某人
     * @date 16:53 2021/10/28
     * @param
     * @return {@link }
     **/
    @RequestMapping("/index")
    @PreAuthorize("hasAnyAuthority('1020')")
    public String index(){

        return "/role/role";
    }


   /**
     * @description 查询角色列表
     * @author 莫某人
     * @date 16:56 2021/10/28
     * @param
     * @return {@link }
     **/
   @RequestMapping("/list")
   @ResponseBody
   @PreAuthorize("hasAnyAuthority('102001')")
   public RespDataBean<Role> roleList(RoleQuery roleQuery){
       return roleService.roleList(roleQuery);
   }

    /**
     * @description 添加|修改角色页面
     * @author 莫某人
     * @date 16:56 2021/10/28
     * @param
     * @return {@link }
     **/
    @RequestMapping("/addOrUpdateRolePage")
    @PreAuthorize("hasAnyAuthority('102002','102003')")
    public String addOrUpdateRole(Long id, Model model){

        if (id==null){

            //添加角色

            return "/role/add_update";
        }else {
            //修改角色
            Role role = roleService.getById(id);
            model.addAttribute("role",role);
            return "/role/add_update";
        }

    }

    /**
     * @description 添加角色
     * @author 莫某人
     * @date 18:09 2021/10/28
     * @param role
     * @return {@link }
     **/
    @RequestMapping("/save")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('102002')")
    public RespBean saveRole(Role role){

        roleService.saveRole(role);


        return  RespBean.success("添加成功!");
    }

    /**
     * @description 角色修改
     * @author 莫某人
     * @date 18:23 2021/10/28
     * @param role
     * @return {@link }
     **/
    @RequestMapping("/update")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('102003')")
    public RespBean updateRole(Role role){
        roleService.updateRole(role);
        return  RespBean.success("修改成功!");
    }

    /**
     * @description 删除角色信息
     * @author 莫某人
     * @date 18:31 2021/10/28
     * @param id
     * @return {@link }
     **/
    @RequestMapping("/delete")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('102004')")
    public RespBean deleteRole(Integer id){
        roleService.deleteRole(id);
        return  RespBean.success("删除角色成功!");
    }

    /**
     * @description 获取角色列表
     * @author 莫某人
     * @date 19:14 2021/10/28
 * @param
     * @return {@link }
     **/
    @RequestMapping("/queryAllRoles")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('102001')")
    public List<Map<String, Object>> queryAllRoles(Integer userId){
       return roleService.queryAllRoles(userId);
    }

    /**
     * @description 角色授权页面
     * @author 莫某人
     * @date 10:36 2021/10/29
 * @param
     * @return {@link }
     **/
    @RequestMapping("toAddGrantPage")
    @PreAuthorize("hasAnyAuthority('1020')")
    public String toAddGrantPage(Integer roleId,Model model){
        model.addAttribute("roleId",roleId);
        return "/role/grant";
    }


    /**
     * @description 对角色添加授权
     * @author 莫某人
     * @date 13:51 2021/10/29
 * @param
     * @return {@link }
     **/

    @RequestMapping("/addGrant")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('102005')")
    public RespBean addGrant(Integer roleId,Integer[] mids){
        roleService.addGrant(roleId,mids);

        return RespBean.success("操作成功!");
    }


}
